library(tidyHeatmap)
library(grid)

mtcars_tidy <- mtcars |>
  as_tibble(rownames = "Car name") |>
  # Scale
  mutate_at(vars(-`Car name`, -hp, -vs), scale) |>
  # tidyfy
  pivot_longer(cols = -c(`Car name`, hp, vs), names_to = "Property", values_to = "Value")

mtcars_tidy

# plot -------
mtcars_tidy |>
  heatmap(`Car name`, Property, Value, scale = "row")

mtcars_tidy |>
  heatmap(`Car name`, Property, Value, scale = "row") |>
  add_tile(hp)

## grouping ----
mtcars_tidy_groupings <-
  mtcars_tidy |>
  mutate(property_group = if_else(Property %in% c("cyl", "disp"), "Engine", "Other"))

### on grouped tibble
mtcars_tidy_groupings |>
  group_by(vs, property_group) |>
  heatmap(`Car name`, Property, Value, scale = "row") |>
  add_tile(hp)

mtcars_tidy_groupings |>
  group_by(vs, property_group) |>
  heatmap(
    `Car name`, Property, Value,
    scale = "row",
    palette_grouping = list(
      # For first grouping (vs)
      c("green4", "red"),
      # For second grouping (property_group)
      c("orange", "blue")
    )
  ) |>
  add_tile(hp)

### split on cladogram
mtcars_tidy |>
  heatmap(`Car name`, Property, Value, scale = "row") |>
  split_rows(2) |>
  split_columns(2)

### split on kmeans
mtcars_tidy |>
  heatmap(
    `Car name`, Property, Value,
    scale = "row",
    row_km = 2,
    column_km = 2
  )

## palette -------
mtcars_tidy |>
  heatmap(
    `Car name`,
    Property,
    Value,
    scale = "row",
    palette_value = c("red", "white", "blue")
  )

mtcars_tidy |>
  heatmap(
    `Car name`,
    Property,
    Value,
    scale = "row",
    palette_value = circlize::colorRamp2(
      seq(-2, 2, length.out = 11),
      RColorBrewer::brewer.pal(11, "RdBu")
    )
  )

mtcars_tidy |>
  heatmap(
    `Car name`,
    Property,
    Value,
    scale = "row",
    palette_value = circlize::colorRamp2(c(-2, -1, 0, 1, 2), viridis::magma(5))
  )

### for annotation
mtcars_tidy |>
  heatmap(
    `Car name`,
    Property,
    Value,
    scale = "row"
  ) |>
  add_tile(
    hp,
    palette = circlize::colorRamp2(c(0, 100, 200, 300), viridis::magma(4))
  )

## multiple annotation -------
pasilla |>
  group_by(location, type) |>
  heatmap(
    .column = sample,
    .row = symbol,
    .value = `count normalised adjusted`,
    scale = "row"
  ) |>
  add_tile(condition) |>
  add_tile(activation)

pasilla |>
  group_by(location, type) |>
  heatmap(
    .column = sample,
    .row = symbol,
    .value = `count normalised adjusted`,
    scale = "row",
    show_heatmap_legend = FALSE
  ) |>
  add_tile(condition, show_legend = FALSE) |>
  add_tile(activation, show_legend = FALSE)

## annotation types ------
# Create some more data points
pasilla_plus <-
  pasilla |>
  mutate(act = activation) |>
  nest(data = -sample) |>
  mutate(size = rnorm(n(), 4, 0.5)) |>
  mutate(age = runif(n(), 50, 200)) |>
  unnest(data)

# Plot
pasilla_plus |>
  heatmap(
    .column = sample,
    .row = symbol,
    .value = `count normalised adjusted`,
    scale = "row"
  ) |>
  add_tile(condition) |>
  add_point(activation) |>
  add_tile(act) |>
  add_bar(size) |>
  add_line(age)

# annotation sizes
## using grid library (rewrite of graphics library)
pasilla_plus |>
  heatmap(
    .column = sample,
    .row = symbol,
    .value = `count normalised adjusted`,
    scale = "row"
  ) |>
  add_tile(condition, size = unit(0.3, "cm"), annotation_name_gp = gpar(fontsize = 8)) |>
  add_point(activation, size = unit(0.3, "cm"), annotation_name_gp = gpar(fontsize = 8)) |>
  add_tile(act, size = unit(0.3, "cm"), annotation_name_gp = gpar(fontsize = 8)) |>
  add_bar(size, size = unit(0.3, "cm"), annotation_name_gp = gpar(fontsize = 8)) |>
  add_line(age, size = unit(0.3, "cm"), annotation_name_gp = gpar(fontsize = 8))

# patchworked
p_heatmap <- heatmap(mtcars_tidy, `Car name`, Property, Value, scale = "row")

p_heatmap + p_heatmap

## cell borders ----
mtcars_tidy |>
  heatmap(
    `Car name`, Property, Value,
    scale = "row",
    rect_gp = grid::gpar(lwd = 0.5)
  )

## disable clustering -----
mtcars_tidy |>
  heatmap(
    `Car name`, Property, Value,
    scale = "row",
    cluster_rows = FALSE
  )

## reorder row/col -------
mtcars_tidy |>
  mutate(`Car name` = fct_reorder(`Car name`, `Car name`, .desc = TRUE)) %>%
  heatmap(
    `Car name`, Property, Value,
    scale = "row",
    cluster_rows = FALSE
  )

## size of dendrograms ----
mtcars_tidy |>
  mutate(`Car name` = fct_reorder(`Car name`, `Car name`, .desc = TRUE)) %>%
  heatmap(
    `Car name`, Property, Value,
    scale = "row",
    column_dend_height = unit(0.2, "cm"),
    row_dend_width = unit(0.2, "cm")
  )

## size of row/col title & names ----
mtcars_tidy |>
  mutate(`Car name` = fct_reorder(`Car name`, `Car name`, .desc = TRUE)) %>%
  heatmap(
    `Car name`, Property, Value,
    scale = "row",
    row_names_gp = gpar(fontsize = 7),
    column_names_gp = gpar(fontsize = 7),
    column_title_gp = gpar(fontsize = 7),
    row_title_gp = gpar(fontsize = 7)
  )

## change sides of legends --------
heatmap(mtcars_tidy, `Car name`, Property, Value, scale = "row") %>%
  as_ComplexHeatmap() %>%
  ComplexHeatmap::draw(heatmap_legend_side = "left")
